package com.jfire.codejson.methodinfo.impl.write;

import java.lang.reflect.Method;
import com.jfire.codejson.util.NameTool;

/**
 * 如果方法的返回对象实现了Iterable接口，则使用该方法返回热编译代码
 *
 */
public class ReturnIterableMethodInfo extends AbstractWriteMethodInfo
{
    public ReturnIterableMethodInfo(Method method)
    {
        super(method);
        str = "Iterable " + NameTool.getNameFromMethod(method) + " = " + getValue + ";\n";
        str += "if(" + NameTool.getNameFromMethod(method) + "!=null)\n{\n";
        str += "\tcache.append(\"\\\"" + NameTool.getNameFromMethod(method) + "\\\":[\");\n";
        str += "\tIterator it =" + NameTool.getNameFromMethod(method) + ".iterator();\n";
        str += "\tObject valueTmp = null;\n";
        str += "\twhile(it.hasNext())\n\t{\n";
        str += "\t\tif((valueTmp=it.next())!=null)\n";
        str += "\t\t{\n";
        str += "\t\t\tif(valueTmp instanceof String)\n";
        str += "\t\t\t{\n";
        str += "\t\t\t\tcache.append('\\\"').append((String)valueTmp).append('\\\"');\n";
        str += "\t\t\t}\n";
        str += "\t\t\telse\n";
        str += "\t\t\t{\n";
        str += "\t\t\t\tWriterContext.write(valueTmp,cache);\n";
        str += "\t\t\t}\n";
        str += "\t\t\tcache.append(',');\n";
        str += "\t\t}\n";
        str += "\t}\n";
        str += "\tif(cache.isCommaLast()){cache.deleteLast();}\n";
        str += "\tcache.append(\"],\");\n";
        str += "}\n";
      
    }
    
}
